package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierServices;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Supplier;
import com.goods.common.model.system.User;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/8  11:44
 * @description：TODO
 */
@Service
public class SupplierServicesImpl implements SupplierServices {
    @Autowired(required = false)
    private SupplierMapper supplierMapper;
    //按条件查询物资来源
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Supplier.class);
        String address = supplierVO.getAddress();
        String contact = supplierVO.getContact();
        String name = supplierVO.getName();
        Example.Criteria criteria = o.createCriteria();
        if(address!=null&&!"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if(contact!=null&&!"".equals(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }
        if(name!=null&&!"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }


        List<Supplier> supplierList = supplierMapper.selectByExample(o);

        List<SupplierVO> supplierVOS = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO1 = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO1);
            return supplierVO1;
        }).collect(Collectors.toList());

        PageInfo<Supplier> info=new PageInfo(supplierList);
        return new PageVO<>(info.getTotal(),supplierVOS);

    }
    //添加物资来源
    @Transactional
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }
    //物资来源回显
    @Transactional
    @Override
    public SupplierVO edit(Long id) throws BusinessException{
        Supplier supplier = new Supplier();
        supplier.setId(id);
        Supplier supplier1 = supplierMapper.selectOne(supplier);
        if(supplier1==null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"要编辑的物资来源不存在");
        }
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier1,supplierVO);
        return supplierVO;
    }
    //修改物资来源信息
    @Override
    public void update(Long id, SupplierVO supplierVO) {
        supplierVO.setModifiedTime(new Date());
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        Example o = new Example(supplier.getClass());
        o.createCriteria().andEqualTo("id",id);
        supplierMapper.updateByExample(supplier,o);
    }
    //删除物资来源
    @Override
    public void delete(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplierMapper.delete(supplier);
    }

    //查询全部数据
    //已知来源
    @Override
    public List<SupplierVO> findAll() {
        Example o = new Example(Supplier.class);
        List<Supplier> supplierList = supplierMapper.selectByExample(o);
        List<SupplierVO> supplierVoList = supplierList.stream().map(
                supplier -> {
                    SupplierVO supplierVO = new SupplierVO();
                    BeanUtils.copyProperties(supplier, supplierVO);
                    return supplierVO;
                }
        ).collect(Collectors.toList());
        return supplierVoList;
    }
}
